﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using EveryBookies.ViewModel;
using Microsoft.Practices.ServiceLocation;

namespace EveryBookies.View
{
    public partial class MainPage : PhoneApplicationPage
    {
        public IBookiesViewModel BookiesViewModelData
        {
            get
            {
                return ServiceLocator.Current.GetInstance<IBookiesViewModel>();
            }
        }
        private int _currentPage = -1;
        public MainPage()
        {
            InitializeComponent();
            this.Loaded += MainPage_Loaded;
        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            offerControl.Visibility = Visibility.Visible;
            _currentPage = 0;
        }
        public void OffersClick()
        {
            if (offerControl.homeBT.Visibility == System.Windows.Visibility.Visible)
            {
                offerControl.homeBT.Visibility = System.Windows.Visibility.Collapsed;
                offerControl.CloseWebView();
            }
            else
            HandleOffers();
        }

        private void HandleOffers()
        {
            bookiesControl.Visibility = Visibility.Collapsed;
            favoriteControl.Visibility = Visibility.Collapsed;
            _currentPage = 0;
            if (offerControl.showWebViewOffer.Visibility == System.Windows.Visibility.Collapsed && offerControl.Visibility != Visibility.Visible)
            {
                BookiesViewModelData.Offers.Clear();
                BookiesViewModelData.Banners.Clear();
                BookiesViewModelData.GetOffer(App.ItemsCountDefault.ToString(), "0");
            }
            offerControl.Visibility = Visibility.Visible;
        }
        private void Offers_Click(object sender, EventArgs e)
        {

            HandleOffers();
        }

        private void Bookies_Click(object sender, EventArgs e)
        {
            offerControl.Visibility = Visibility.Collapsed;
           
            favoriteControl.Visibility = Visibility.Collapsed;
            _currentPage = 1;
            if (offerControl.showWebViewOffer.Visibility == System.Windows.Visibility.Collapsed && bookiesControl.Visibility != Visibility.Visible)
            {
                BookiesViewModelData.Bookies.Clear();
                BookiesViewModelData.Banners.Clear();
                BookiesViewModelData.GetBookies(App.ItemsCountDefault.ToString(), "0");
            }
            bookiesControl.Visibility = Visibility.Visible;
        }

        private void Favourites_Click(object sender, EventArgs e)
        {
            offerControl.Visibility = Visibility.Collapsed;
            bookiesControl.Visibility = Visibility.Collapsed;
            favoriteControl.Visibility = Visibility.Visible;
            _currentPage = 2;
            BookiesViewModelData.Favourites.Clear();
            BookiesViewModelData.GetFavorites();
        }

        private void PhoneApplicationPage_BackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
        {
            switch (_currentPage)
            {
                case 0:
                    if (offerControl.CloseWebView())
                    {
                        e.Cancel = true;
                    }
                    else
                        Application.Current.Terminate();
                    break;
                case 1:
                    if (bookiesControl.CloseWebView())
                    {
                        e.Cancel = true;
                    }
                    else
                        Application.Current.Terminate();
                    break;
                case 2:
                    if (favoriteControl.CloseWebView())
                    {
                        e.Cancel = true;
                    }
                    else
                        Application.Current.Terminate();
                    break;
            }
        }
    }
}